package com.scuvc.utils;

import com.scuvc.entity.CarInfo;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class JMenuBarUtils {
    /**
     * 封装通用的菜单
     * @param JMenuName 菜单名
     * @param x 菜单显示x轴
     * @param y 菜单显示y轴
     * @param jMenuItemName 菜单的下拉列表
     * @return 菜单对象
     */
    public static JMenuBar createJMenuBar(List<String> JMenuName,int x, int y,String[][] jMenuItemName){
        JMenu menu = null;
        JMenuBar jMenuBar = new JMenuBar();
        List<JMenu> menuList = new ArrayList<>();
        StaticUtils.jmiList = new JMenuItem[jMenuItemName.length][];
        for(int i=0;i<JMenuName.size();i++){
            StaticUtils.jmiList[i] = new JMenuItem[jMenuItemName[i].length];
        }
        for(int i=0;i<JMenuName.size();i++){
            menu = new JMenu(JMenuName.get(i));
            //设置弹出的位置
            menu.setMenuLocation(x,y);
            for(int j=0;j<jMenuItemName[i].length;j++){
                JMenuItem jMenuItem = new JMenuItem(jMenuItemName[i][j]);
                menu.add(jMenuItem);
                StaticUtils.jmiList[i][j] = jMenuItem ;
            }
            menuList.add(menu);
        }
        jMenuBar.setPreferredSize(new Dimension(65,20));
        jMenuBar.setLayout(new GridLayout(4,1));
        jMenuBar.setOpaque(false);
        for(int i=0;i<menuList.size();i++){
            jMenuBar.add(menuList.get(i));
            menuList.get(i).setBorder(BorderFactory.createLineBorder(Color.BLACK));
        }
        return jMenuBar;
    }

    public static void setData(List<JLabel> list, List<CarInfo> carList,JPanel panCenter,String account){
        if(account.equals("admin")){
            for(int i=0;i<carList.size();i++){
                list.add(new JLabel(carList.get(i).getId()+""));
                list.add(new JLabel(carList.get(i).getCarNo()+""));
                list.add(new JLabel(carList.get(i).getBrand().getBrand_name()));
                list.add(new JLabel(carList.get(i).getModel().getModel_name()));
                list.add(new JLabel(carList.get(i).getCategory().getCategory_name()));
                list.add(new JLabel(carList.get(i).getDisplacement()));
                list.add(new JLabel(carList.get(i).getPrice()+""));
                list.add(new JLabel(carList.get(i).getCar_amount()+""));
                list.add(new JLabel(carList.get(i).getDeposit()+""));
                list.add(new JLabel(carList.get(i).getStatus()==1?"可租":"已租"));
                list.add(new JLabel(carList.get(i).getUsable()==1?"上架":"下架"));
            }
        }else{
            for(int i=0;i<carList.size();i++){
                list.add(new JLabel(carList.get(i).getId()+""));
                list.add(new JLabel(carList.get(i).getCarNo()+""));
                list.add(new JLabel(carList.get(i).getBrand().getBrand_name()));
                list.add(new JLabel(carList.get(i).getModel().getModel_name()));
                list.add(new JLabel(carList.get(i).getCategory().getCategory_name()));
                list.add(new JLabel(carList.get(i).getDisplacement()));
                list.add(new JLabel(carList.get(i).getPrice()+""));
                list.add(new JLabel(carList.get(i).getCar_amount()+""));
                list.add(new JLabel(carList.get(i).getDeposit()+""));
                list.add(new JLabel(carList.get(i).getStatus()==1?"可租":"已租"));
            }
        }
        Font font = new Font("仿宋",Font.PLAIN,18);
        for(int i=0;i<list.size();i++){
            list.get(i).setFont(font);
            panCenter.add(list.get(i));
            //设置标签的边框线
            list.get(i).setBorder(BorderFactory.createLineBorder(Color.BLACK));
            //设置标签的列宽
            list.get(i).setPreferredSize(new Dimension(
                    108,40));
            list.get(i).setHorizontalAlignment(SwingConstants.CENTER);
        }
    }
}
